package com.groupbuying.redpacket.controller;

import com.groupbuying.redpacket.context.FilterContextHandler;
import com.groupbuying.redpacket.domain.RedPacketListDO;
import com.groupbuying.redpacket.service.RedPacketListService;
import com.groupbuying.redpacket.service.impl.RedPacketListServiceImpl;
import com.groupbuying.redpacket.utils.PageUtils;
import com.groupbuying.redpacket.utils.Query;
import com.groupbuying.redpacket.vo.RedPacketListVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author yangmeng
 * @Date 2019/11/15 9:38
 */
@RestController
@RequestMapping("/redPacket")
public class RedPacketListController {

    private static final Logger logger = LoggerFactory.getLogger(RedPacketListServiceImpl.class);

    @Autowired
    private RedPacketListService redPacketListService;

    /**
     * @api {get} /redPacket/getRankingList 屏幕WEB端轮次活动结束排行榜
     * @apiExample
     * author yangmeng
     * tar 用于大屏幕WEB端
     * date 2019/11/15 15:20
     * @apiGroup service-redpacket
     * @apiVersion 1.0.0
     * @apiParam {String} taskId 红包任务id
     * @apiParam {String} platFrom 活动分类
     * @apiSuccess {Map} currentRankingRotation 当前分类当前轮次排行榜
     * @apiSuccess {Map} theTotalList 当前分类总排行榜
     * @apiSuccess {Map} themeActivities 当前分类当前轮次排行榜标题
     */
    @GetMapping("/getRankingList")
    public Map getRankingList(String taskId, String platFrom) {
        logger.info("获取taskId的值:{}", taskId);
        Map<String, Object> map = new HashMap<>();
        //查询当前分类当前轮次排行榜
        List<Map<String, Object>> currentRankingRotation = redPacketListService.currentRankingRotation(taskId);
        //查询当前分类总排行榜
        List<Map<String, Object>> theTotalList = redPacketListService.theTotalList(platFrom);
        //查询当前分类当前轮次排行榜标题
        Map<String, Object> themeActivities = redPacketListService.themeActivities(taskId, platFrom);
        //当前分类当前轮次排行榜、当前分类总排行榜、当前分类当前轮次排行榜标题放入map
        map.put("currentRankingRotation", currentRankingRotation);
        map.put("theTotalList", theTotalList);
        map.put("themeActivities", themeActivities);
        return map;
    }

    /**
     * @api {get} /redPacket/getRedEnvelopeRecord 微信小程序摇一摇抢红包中奖纪录
     * @apiExample
     * author yangmeng
     * tar 用于微信小程序客户端
     * date 2019/11/21 13:54
     * @apiGroup service-redpacket
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiSuccess {Map} winningRecord 中奖纪录信息
     * @apiSuccess {Map} redPacketCount 红包个数
     * @apiSuccess {Map} redPacketMoney 红包总金额
     */
    @GetMapping("/getRedEnvelopeRecord")
    public Map getRedEnvelopeRecord(@RequestParam Map<String, Object> params) {
        String userId = FilterContextHandler.getUserID();
        logger.info("获取userId的值:{}", userId);
        //建空Map
        Map<String, Object> map = new HashMap<>();
        //判断userId为空的情况
        if (StringUtils.isEmpty(userId)) {
            logger.error("红包信息,userId不能为空或者不能为null", userId);
            map.put("winningRecord", new PageUtils(new ArrayList<>(),0));
            map.put("redPacketCount", 0);
            map.put("redPacketMoney", 0.00);
            return map;
        }
        params.put("userId",userId);
        //查询中奖纪录信息
        Query query = new Query(params);
        List<RedPacketListVO> redPacketListVOList = redPacketListService.winningRecord(query);
        int total = redPacketListService.winningRecordCount(params);
        PageUtils pageUtils = new PageUtils(redPacketListVOList, total);
        //查询红包个数和红包总金额
        Map<String, Object> redPacketNumberMoney = redPacketListService.redPacketNumberMoney(userId);
        //中奖纪录信息、红包个数、红包总金额放入map
        map.put("winningRecord", pageUtils);
        map.put("redPacketCount", redPacketNumberMoney.get("redPacketCount"));
        map.put("redPacketMoney", redPacketNumberMoney.get("redPacketMoney"));
        return map;
    }

    /**
     * @api {get} /redPacket/allWinningRecords 屏幕WEB端活动中本轮中奖纪录
     * @apiExample
     * author yangmeng
     * tar 用于大屏幕WEB端
     * date 2019/11/22 12:03
     * @apiGroup service-redpacket
     * @apiVersion 1.0.0
     * @apiParam {String} taskId 红包任务id
     * @apiSuccess {PageUtils} redPacketListVOList 本轮中奖纪录
     */
    @GetMapping("/allWinningRecords")
    public PageUtils allWinningRecords (@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        //查询本轮中奖纪录
        List<RedPacketListVO> redPacketListVOList = redPacketListService.allWinningRecords(query);
        int total = redPacketListService.allWinningRecordsCount(params);
        PageUtils pageUtils = new PageUtils(redPacketListVOList, total);
        return pageUtils;
    }

    /**
     * @api {get} /redPacket/currentRankingRotation 大屏幕WEB端活动中当前轮次排行榜
     * @apiExample
     * author yangmeng
     * tar 用于大屏幕WEB端
     * date 2019/11/25 8:43
     * @apiGroup service-redpacket
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiParam {String} taskId 活动id
     * @apiParam {String} platFrom 活动分类
     * @apiSuccess {Map} currentRankingRotation 当前分类当前轮次排行榜
     */
    @GetMapping("/currentRankingRotation")
    public Map currentRankingRotation (String taskId) {
        logger.info("获取taskId的值:{}", taskId);
        Map<String, Object> map = new HashMap<>();
        //查询当前分类当前轮次排行榜
        List<Map<String, Object>> currentRankingRotation = redPacketListService.activityCurrentRankingRotation(taskId);
        //当前分类当前轮次排行榜放入map
        map.put("currentRankingRotation", currentRankingRotation);
        return map;
    }

    /**
     * @api {get} /redPacket/theWinningRecord 管理后台WEB端中奖纪录
     * @apiExample
     * author yangmeng
     * tar 用于管理后台WEB端
     * date 2019/11/25 8:53
     * @apiGroup service-redpacket
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiSuccess {pageUtils} redPacketListVOList 中奖纪录
     */
    @GetMapping("/theWinningRecord")
    public PageUtils theWinningRecord (@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        //查询中奖纪录
        List<RedPacketListVO> redPacketListVOList = redPacketListService.theWinningRecord(query);
        int total = redPacketListService.theWinningRecordCount(params);
        PageUtils pageUtils = new PageUtils(redPacketListVOList, total);
        return pageUtils;
    }

    /**
     * @api {get} /redPacket/redPacketTheWinningStatistics 管理后台WEB端中奖统计
     * @apiExample
     * author yangmeng
     * tar 用于管理后台WEB端
     * date 2019/11/25 9:09
     * @apiGroup service-redpacket
     * @apiVersion 1.0.0
     * @apiHeader {String} Authorization 用户授权token
     * @apiSuccess {Map} theWinningStatistics 中奖统计列表
     * @apiSuccess {Map} categorizationNumberAmount 平台分类红包金额和红包个数
     */
    @GetMapping("/redPacketTheWinningStatistics")
    public Map redPacketTheWinningStatistics (@RequestParam Map<String, Object> params) {
        Map<String, Object> map = new HashMap<>();
        //查询中奖统计列表
        Query query = new Query(params);
        List<RedPacketListVO> redPacketListVOList = redPacketListService.theWinningStatistics(query);
        int total = redPacketListService.theWinningStatisticsCount(params);
        PageUtils pageUtils = new PageUtils(redPacketListVOList, total);
        //查询平台分类红包金额和红包个数
        List<Map<String, Object>> categorizationNumberAmount = redPacketListService.categorizationNumberAmount(params);
        //中奖统计列表，平台分类红包金额和红包个数放入map
        map.put("theWinningStatistics", pageUtils);
        map.put("categorizationNumberAmount", categorizationNumberAmount);
        return map;
    }

}
